require(['grid', 'topBar', 'layout','validator'], function () {
    var previewDialog, 
        editDialog,
        $grid = $('#grid'),
        reload,
        $search = $('#search'),
        appUpgrade="5",
        $addSet=$("#addSet"),
        type,lineId,carId;

    //左侧树
    commonFun.leftTreeNodeClickObj['UpgradeByFile'] = function (event, treeId, treeNode) {
        if(treeNode.type==='LINE'){
            type="LINE";
            lineId = treeNode.id;
        }else if(treeNode.type==='CAR'){
            type="CAR";
            carId = treeNode.id;
        }
    };

    //顶部类型
    $(".element-select a").click(function(){
        appUpgrade = $(this).attr('val');
        $(this).addClass("active");
        $(this).siblings().removeClass("active");
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData:  {"vrsnName" : $("#vrsnName").val(), "appUpgrade" : appUpgrade}
        }).trigger("reloadGrid");
    });

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/upgrade/getByFilePage.htm",
            colModel: [
                {name: "vrsnId", label: "vrsnId", key: true, hidden: true},
                {name: "vrsnName", label: "版本名称"},
                {name: "remark", label: "备注"},
                {name: "operate", width: 130, label: '操作', title: false,align:'center',
                    formatter: 'operArea',
                    formatoptions: [
                        {
                            text: '预览',
                            css:'operate-edit',
                            click: function (data) {
                                var dlg=dialogPreview();
                                dlg.dialog('exec', 'setVal',data.vrsnId);
                                dlg.dialog('open');
                            }
                        },
                        {
                            text: '删除',
                            css:'operate-delete',
                            click: function (data) {
                            $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                                if (r) {
                                    console.info(data)
                                	$.ajaxExt({
                                        url : "/upgrade/delUpgrade.htm",
                                        data : data
            						}).done(function(){
                                        reload2();
            						});
                                }
                            });
                        }
                    }]
            
                }
            ],
            multiselect: false,
            height:$(window).height()-180
        });
    }

    /**
     * 新增按钮
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    $addSet.click(function() {
        var type = commonFun.getLeftTreeCurrentTreeType();
        if(type=='CAR'){
            var orgIds = commonFun.getLeftTreeCheckedNodes(type);
            if(!orgIds){
                $.messager.alert("提示","请先选择车辆");
                return;
            }
            if(appUpgrade=='7'){//唯一参数,只能设置单台车
                if(!orgIds[0]){
                    $.messager.alert("提示","请先选择车辆");
                    return;
                }
                else if(orgIds.length>1){
                    $.messager.alert("提示","请先选择一辆车");
                    return;
                }
            }
            var title="";
            classCd = "";
            switch(appUpgrade){
                case "5":
                    title="新增通讯参数";
                    break;
                case "6":
                    title="新增外设参数";
                    break;
                case "7":
                    title="新增唯一参数";
                    break;
                case "8":
                    title="新增行为参数";
                    break;
                case "9":
                    title="新增线路参数";
                    break;
                case "10":
                    title="新增发动机参数";
                    break;
                case "11":
                    title="新增电机参数";
                    break;
                case "12":
                    title="终端控制";
                    break;
            }
            var dlg=getEditDialog(title);
            dlg.dialog('exec', 'setParam',appUpgrade);
            dlg.dialog('open');
        }
        else{
            $.messager.alert("提示","请先选择车辆");
        }
        // if(type=="LINE" && nodes[0]){
        //     if(appUpgrade=="5") {
        //         getEditDialog('新增通讯参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam', "5","通讯参数");
        //     }else if(appUpgrade=="6"){
        //         getEditDialog('新增外设参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"6");
        //     }else if(appUpgrade=="8"){
        //         getEditDialog('新增行为参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"8");
        //     }else if(appUpgrade=="9"){
        //         getEditDialog('新增线路参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"9");
        //     }else if(appUpgrade=="10"){
        //         getEditDialog('新增发动机参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"10");
        //     }else if(appUpgrade=="11"){
        //         getEditDialog('新增电机参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"11");
        //     }else if(appUpgrade=="7"){
        //         $.messager.alert("提示","请选择车辆新增");
        //         return;
        //     }
        // }else if(type=="CAR" && nodes[0]){
        //     if(appUpgrade=="7"){
        //         getEditDialog('新增唯一参数','./upgrade/setting/UpgradeFileSetComm.htm').dialog('exec', 'setParam',"7");
        //     }else{
        //         $.messager.alert("提示","请选择线路新增");
        //         return;
        //     }
        // }else{
        //     $.messager.alert("提示","请选择线路或车辆新增");
        //     return;
        // }
    });

    function getEditDialog(title){
        if(!editDialog){
            editDialog = $('<div>').dialog({
                title:title,
                url: './upgrade/setting/UpgradeFileSetComm.htm',
                height: 550,
                width: 800,
                buttons: [
                    {
                        text: '下发',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', 'add',function(){
                                reload(true);
                            });
                        }
                    },
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        editDialog.dialog('setOption', 'title',title);
        return editDialog;
    }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    // function getEditDialog(title,url) {
    //     editDialog = $('<div>').dialog({
    //         title:title,
    //         url: url,
    //         height: 550,
    //         width: 800,
    //         autoOpen: true,
    //         buttons: [
    //             {
    //                 text: '下发',
    //                 css: 'mr10',
    //                 click: function () {
    //                     var _t = this;
    //                      $(_t).dialog('exec', 'add',function(){
    //                         reload(true);
    //                     });
    //                 }
    //             },
    //             {
    //                 text: '关闭',
    //                 css: 'btn-cancel',
    //                 click: function () {
    //                     $(this).dialog('close');
    //                 }
    //             }
    //         ]
    //     });
    //     return editDialog;
    // }

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function dialogPreview() {
        if(!previewDialog){
            previewDialog = $('<div>').dialog({
                title:  '预览升级命令',
                url: './upgrade/setting/UpgradeByFileEdit.htm',
                height: 550,
                width: 720,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        return previewDialog;
    }

    //查询grid
    $search.on("click", function () {
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData:  {"vrsnName" : $("#vrsnName").val(), "appUpgrade" : appUpgrade}
        }).trigger("reloadGrid");
    });

    //新增修改后执行
    function reload(close){
        close && editDialog.dialog('close');
        var data = {};
        // if(type=="LINE"){
        //     data.lineIds = lineId;
        // }else if(type=="CAR"){
        //     data.carIds = carId;
        // }
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        data.type=type;
        data.orgIds=orgIds.toString();
        data.appUpgrade=appUpgrade;
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData:  {"vrsnName" : $("#vrsnName").val(), "appUpgrade" : appUpgrade}
        }).trigger("reloadGrid");
        // 跳转到升级日志
        $.openTab('781ce7c7d2794ca0b8ab2a4473bd8109', 'gotoSearch', [data]);
    };

    function reload2(close){
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData:  {"vrsnName" : $("#vrsnName").val(), "appUpgrade" : appUpgrade}
        }).trigger("reloadGrid");
        close && editDialog.dialog('close');
    };
    gridInit();
    
});